package com.zl.task.util.resp;

import org.apache.poi.ss.formula.functions.T;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * @author YinlinGeng
 */
public class RespUtil {
    private static T data;

    public static <T> RespResult<T> success(T t) {
        RespResult<T> rr = new RespResult<T>();
        rr.setMsg(RespCode.SUCCESS.getMsg());
        rr.setCode(RespCode.SUCCESS.getCode());
        rr.setRespTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        rr.setData(t);
        return rr;
    }

    public static <T> RespResult<T> success(String msg, T t) {
        RespResult<T> rr = new RespResult<T>();
        rr.setMsg(msg);
        rr.setCode(RespCode.SUCCESS.getCode());
        rr.setRespTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        rr.setData(t);
        return rr;
    }

    public static RespResult success() {
        RespResult rr = new RespResult();
        rr.setCode(RespCode.SUCCESS.getCode());
        rr.setMsg(RespCode.SUCCESS.getMsg());
        rr.setRespTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        return rr;
    }


    public static <T> RespResult<T> error(RespCode respCode) {
        RespResult<T> rr = new RespResult<T>();
        rr.setCode(respCode.getCode());
        rr.setMsg(respCode.getMsg());
        rr.setRespTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        return rr;
    }

    public static <T> RespResult<T> error(String msg) {
        RespResult<T> rr = new RespResult<T>();
        rr.setCode(RespCode.HTTP_PARAMS_ERROR.getCode());
        rr.setMsg(msg);
        rr.setRespTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        return rr;
    }

    public static <T> RespResult<T> error(String msg, T data) {
        RespResult<T> rr = new RespResult<T>();
        rr.setCode(RespCode.HTTP_PARAMS_ERROR.getCode());
        rr.setMsg(msg);
        rr.setData(data);
        rr.setRespTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        return rr;
    }

    public static <T> RespResult<T> nulls(String msg) {
        RespResult<T> rr = new RespResult<T>();
        rr.setCode(RespCode.HTTP_PARAMS_ERROR.getCode());
        rr.setMsg("参数：" + msg + "为必填参数");
        rr.setRespTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        return rr;
    }

    public static <T> RespResult<T> success(T t, int count) {
        RespResult<T> rr = new RespResult<T>();
        rr.setMsg(RespCode.SUCCESS.getMsg());
        rr.setCode(RespCode.SUCCESS.getCode());
        rr.setRespTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        rr.setData(t);
        rr.setCount(count);
        return rr;
    }


    public static RespResult success(List<?> data, Integer count) {
        RespResult<Object> rr = new RespResult();
        rr.setCode(RespCode.SUCCESS.getCode());
        rr.setMsg(RespCode.SUCCESS.getMsg());
        rr.setData(data);
        rr.setCount(count);
        rr.setRespTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        return rr;
    }

    /**
     * 只返回count
     *
     * @param count
     * @return
     */
    public static RespResult success(Integer count) {
        RespResult<Object> rr = new RespResult();
        rr.setCode(RespCode.SUCCESS.getCode());
        rr.setMsg(RespCode.SUCCESS.getMsg());
        rr.setData(data);
        rr.setCount(count);
        rr.setRespTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        return rr;
    }
}